* master do-file
********************************************************************************
* prelims
********************************************************************************
{
clear all
cap log close
set more off

*** directories ***
* path to main directory below:
local user "path_to_main_directory_here"

* globals
global data "`user'/data"
global prog "`user'/prog"
global orig "`user'/orig"
global tables "`user'/tables"
global figs "`user'/figs"
global dir "`user'"
cd "$dir"

}
********************************************************************************
* do-files
********************************************************************************
{

* 1. Clean MCVL.
* main files generated:
* (1) mcvl2004-17_01pers.dta -> table 1: personal information
* (2) mcvl2004-17_02affi.dta -> table 2: work histories
* (3) mcvl2004-17_03cont.dta -> table 3: contributions
* (4) mcvl2004-17_04pens.dta -> table 4: pensions
* (5) mcvl2004-17_05coha.dta -> table 5: cohabitants
* (6) mcvl2004-17_06fisc.dta -> table 6: fiscal information
do "$prog/restat_01_mcvl.do"

* 2. Restrict the sample according to the following criteria:
* (1) born before Jan 1st, 1973
* (2) entering unemployment at some point in the working history
* main files generated:
* (1) mcvl2004-17_01pers_rest_age_un.dta -> table 1: personal information
* (2) mcvl2004-17_02affi_rest_age_un.dta -> table 2: work histories
* (3) mcvl2004-17_03cont_rest_age_un.dta -> table 3: contributions
* (4) mcvl2004-17_04pens_rest_age_un.dta -> table 4: pensions
* (5) mcvl2004-17_05coha_rest_age_un.dta -> table 5: cohabitants
* (6) mcvl2004-17_06fisc_rest_age_un.dta -> table 6: fiscal information
* (7) mcvl2004-17_01pers_rest_age_un_long.dta -> table 1 in long format
* (8) mcvl2004-17_03cont_rest_age_un_wide.dta -> table 3 in wide format
do "$prog/restat_02_mcvl_sample.do"

* 3. Clean and generate new variables. Store in intermediate datasets.
* main files generated:
* (1) int/birthdate.dta -> birthdate info
* (2) int/spellgaps_nover.dta -> spells without overlaps
* (3) int/ui_entit.dta -> UI entitlements
* (4) int/fijosd_spells.dta -> identify fijo discontinuo spells
* (5) int/industry_spell.dta -> industry variables
* (6) int/pensions.dta -> pensions (DI) information
* (7) int/n_cohabitants.dta -> cohabitants
* (8) int/urates.dta -> quarterly unemployment rates
do "$prog/restat_03_mcvl_int_data.do"

* 4. Clean and generate new variables from non-MCVL datasets.
* main files generated:
* (1) int/inflation.dta -> inflation data from FRED
* (2) int/ui_ua_series.dta -> UI/UA series (from SS)
* (3) int/GeoDistSUB.dta -> subsidy shares in province
do "$prog/restat_04_add_data.do"

* 5. Generate final dataset for analysis.
* main files generated:
* (1) int/analysis_dataset_spell.dta -> spell-level cleaned data
* (2) analysis_dataset.dta -> non-employment spell-level cleaned data
do "$prog/restat_05_gen_analysis_dataset.do"

* 6. Main analysis.
* produces all tables and figures, except Figure B1
do "$prog/restat_06_analysis.do"

* 7. Produce Figure B1.
rscript using "$prog/restat_07_map.R"

}
********************************************************************************
* closing
********************************************************************************
{
cap log close
clear
}
